var request = require('request');
let data = require('../libs/data');

module.exports = {

    getOpendId: function (req, res) {
        console.log('req: ',req);
        var code  = req.body.code;
        var body = {
            "code": code,
        };
        request({
            url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx1d6e956bc2775a9f&secret=98a1fac2112605018497daaa86b1da90&js_code='+code+'&grant_type=authorization_code',
            method: "POST",
            body: JSON.stringify(body)
        }, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                console.log(response.body);
                data.code = '200';
                data.msg = '获取成功';
                data.data = JSON.parse(response.body).openid;
            } else {
                data.code = '101';
                data.msg = '获取失败';
                data.data = '';
            }
            res.status(200).send(JSON.stringify(data));
        });
    }

};